<?php

/**
 * Created by PhpStorm.
 * User: koastal
 * Date: 2016/5/15
 * Time: 12:03
 */
require_once 'config.php';
class Loader
{
    public  static function baseLoad()
    {
        require_once("Controller.php");
        require_once("Model.php");
    }
    public static function searchFile($filename,$path)
    {
        $filePath = false;
        $list = scandir($path);
        foreach($list as $file){
            if($file=="." || $file=="..") continue;
            $realPath = $path.DIRECTORY_SEPARATOR.$file;
            if(is_dir($realPath)){
                $res = Self::searchFile($filename,$realPath);
                if($res){
                    return $res;
                }
            }elseif($file!='.' && $file!='..'){
                if($file == $filename){
                    $filePath = $realPath;
                    break;
                }
            }
        }
        return $filePath;
    }
    public static function autoload($class)
    {
        $filename = $class.".class.php";
        $cflag = Self::searchFile($filename,Controller_PATH);
        $mfalg = Self::searchFile($filename,Model_PATH);
        $path = false;
        if(!empty($cflag)){
            if(file_exists($cflag)){
                $path = true;
            }
        }

        $path = ($cflag != false)? $cflag:$path;
        $path = ($mfalg != false)? $mfalg:$path;

        if($path == false){
            exit("Class Load Failed.");
        }else{
            require_once($cflag);
        }
    }
}
$Loader = new Loader();
$Loader->baseLoad();
spl_autoload_register(array('Loader', 'autoload'));
